<?php
namespace app\admin\controller;
use app\common\controller\Admin;
use app\common\model\Admin as AdminModel;
use app\common\model\AuthGroupAccess;
use think\Session;
use think\Cookie;

class Manager extends Admin
{
    protected $modelClass ='app\common\model\Admin';
    
    public function index()
    {
        return $this->fetch();
    }
    
    public function add(){
        $params = paramFromPost();
        
        $admin = new AdminModel();
        $params['id'] =null;

        if(isset($params['password'])){
            $params['password'] = md5($params['password']);
        }


        $rt1 = $admin->allowField(true)->save($params);
        
        
        $access = new AuthGroupAccess();
        $access->uid = $admin->id;
        $access->group_id = $params['group'];
        $rt2 = $access->save();
        
        if($rt1&&$rt2){
            success($admin);
        }else{
            error('新增失败了');
        }
    }
    
//    public function filter(){
//
//
//        $filter = array();
//
//        $query = AdminModel::where($filter)->order('create_time desc');
//        $list = $query->paginate(20);
//
//        foreach ($list as $admin){
//            $admin['group'] = $admin->getGroupAttr();
//        }
//
//        success($list);
//    }
    
    
    public function edit_manager(){
        
        $params = paramFromPost();
        
        $modelClass = $this->modelClass;
      
    
        if($params['oper']=='edit'){
            $admin = new $modelClass();

            if(isset($params['password'])){
                $params['password'] = md5($params['password']);
            }
            $rt = $admin->allowField(true)->save($params,$params['id']);
            
            //一个人目前只能在一个用户组里面吧
            if(isset($params['group'])){
                $access = AuthGroupAccess::get(['uid'=>$admin->id]);
                if(!$access){
                    $access = new AuthGroupAccess();
                    $access->uid = $admin->id;
                }
                $access->group_id = $params['group'];
                $access->save();
            }
        }

        
    
        if($params['oper']=='del'){
            
            $admin = $modelClass::get($params['id']);
            $admin->status=0;
            $admin->save();
            
        
        }
    
    
        success($rt);
        
    }
    
    public function login(){

        $this->view->engine->layout(false);
        return $this->fetch();
    }
    
    public function doLogin(){
        
        $params = paramFormPost();

        $admin = AdminModel::get(['username'=>$params['username']]);

        if($admin&&$admin->password==md5($params['password'])){
            session('ke_user_auth',$admin);
            $admin->last_login_time = time();
            $admin->save();
            $this->success('登录成功','/index/index');//,get_redirect_url());
        }else{
            $this->error('账号或者密码错误','/manager/login');
        }
        
        
    }

    public function logout(){
        Session::delete('ke_user_auth');
        Cookie::delete('ke_redirect_url');
        $this->success('退出成功','/manager/login');
    }

}